package cxt.llz.com.mywedding;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import auth.ShowLoginDialog;
import base.BaseActivity;
import bean.User;
import bean.PraiseInfo;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.SaveListener;


public class MainActivity extends BaseActivity {

    //view
    private ImageView ivPraiseIcon;
    private TextView tvPraiseCount;


    //data
    private int mPraiseCount; //点赞次数

    public static final int LOGIN_PRAISE = 1;
    public static final int LOGIN_CAMERA = 1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        Bmob.initialize(this, "9e0aaa6fc05c277b219d3de293c1bcb7");

        initView();
        initListener();

        
    }

    private void initListener() {

        ivPraiseIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showLoginDialog(LOGIN_PRAISE);

            }
        });



    }

    private void showLoginDialog(final int from) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.loginTip);
        View view = LayoutInflater.from(this).inflate(R.layout.login,null);
        final EditText etName = (EditText) view.findViewById(R.id.et_name);
        final EditText etPhone = (EditText) view.findViewById(R.id.et_phone);
        builder.setView(view);
        builder.setPositiveButton("确定发送", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                User user = new User();
                Editable name = etName.getText();
                if(name != null && !name.toString().equals(""))
                {
                    user.setUsername(name.toString());
                }

                Editable phone = etPhone.getText();
                if(phone != null && !phone.toString().equals(""))
                {
                    user.setPassword(phone.toString());
                }


                user.signUp(MainActivity.this, new SaveListener() {
                    @Override
                    public void onSuccess() {
                        toast("forver friendship,forever love!");

                        if (from == LOGIN_PRAISE) {
                            mPraiseCount++;
                            tvPraiseCount.setText("" + mPraiseCount);
                        }
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        toast(getResources().getString(R.string.failuretip));
                    }
                });

            }
        });

        builder.show();
    }

    private void initView() {

        ivPraiseIcon = (ImageView) findViewById(R.id.praise_icon);
        tvPraiseCount = (TextView) findViewById(R.id.praise_tv);

        //查询点赞数
        BmobQuery<PraiseInfo> bmobQuery = new BmobQuery<PraiseInfo>();
        bmobQuery.getObject(this, "J6cT0008", new GetListener<PraiseInfo>() {
            @Override
            public void onSuccess(PraiseInfo object) {
                // TODO Auto-generated method stub
                toast("查询成功");
                tvPraiseCount.setText(object.getCount());
                mPraiseCount = Integer.parseInt(object.getCount());
            }

            @Override
            public void onFailure(int code, String msg) {
                // TODO Auto-generated method stub
                toast("查询失败：" + msg);
            }
        });

        //提示输入信息
        BmobUser bmobUser = BmobUser.getCurrentUser(this);
        if(bmobUser != null)    //有缓存，自动登陆
        {
//            autoLogin();
        }else{                   //无缓冲，登陆界面
//            login();
        }
    }






}
